import Vue from 'vue'
import Vuex from 'vuex'
import tim from '../commen/tim/tim'

Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		isLogin: false,
		isSDKReady: false, // TIM SDK 是否 ready

		conversationActive: {}, //聊天进行中的会话
		toUserId: '', //聊天对象id
		conversationList: [], //会话列表
		currentMessageList: [], //消息列表
		voiceModalShow: false, // 是否显示语音通话弹窗
		voiceChatUser: null, // 拨打电话过来的用户
		voiceCancel: false, // 是否被拒绝
		videoModalShow: false, // 是否显示视频通话弹窗
		videoChatUser: null, // 拨打电话过来的用户
		videoCancel: false, // 视频是否被拒绝
		videoChatBusy: false, // 通话占线
		videoChatIsBusy:false, // 通话是否占线
	}, 
	mutations: {
		setVideoChatIsBusy(state, bool) {
			state.videoChatIsBusy = bool
		},
		setVideoChatBusy(state, bool) {
			state.videoChatBusy = bool
		},
		setVoiceCancel(state, bool) {
			state.voiceCancel = bool
		},
		setVoiceChatUser(state, user) {
			state.voiceChatUser = user
			if (user) {
				uni.setStorageSync('chooseUser', JSON.stringify(user))
			} else {
				uni.removeStorageSync('chooseUser')
			}
		},
		setVideoModalShow(state, bool) {
			state.videoModalShow = bool
		},
		setVideoCancel(state, bool) {
			state.videoCancel = bool
		},
		setVideoChatUser(state, user) {
			state.videoChatUser = user
			if (user) {
				let chooseUser = uni.getStorageSync('chooseUser')
				uni.setStorageSync('chooseUser', JSON.stringify(user))
			} else {
				uni.removeStorageSync('chooseUser')
			}
		},
		setVoiceModalShow(state, bool) {
			state.voiceModalShow = bool
		},
		//更新登录状态
		toggleIsLogin(state, isLogin) {
			state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
		},
		//更新TIMSDK状态
		toggleIsSDKReady(state, isSDKReady) {
			state.isSDKReady = typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
		},
		//退出登录重置状态
		reset(state) {
			state.isLogin = false
			state.isSDKReady = false
		},
		//选择好友聊天--创建会话/拼接会话id
		createConversationActive(state, toUserId) {
			// console.log("创建会话-----------");
			if (toUserId.type == "C2C") {
				state.conversationActive.conversationID = 'C2C' + toUserId.data
			} else {
				state.conversationActive.conversationID = 'GROUP' + toUserId.data
			}
			state.toUserId = toUserId.data
			state.currentMessageList = []
		},
		//选择已有会话聊天--更新选中会话详情
		updateConversationActive(state, conversationItem) {
			state.conversationActive = Object.assign({}, conversationItem.conversation);
			state.toUserId = conversationItem.user.userId
			state.currentMessageList = []
		},
		//更新会话列表
		updateConversationList(state, newConversationList) {
			state.conversationList = newConversationList
		},
		/**
		 * 将消息插入当前会话列表
		 * 调用时机：收/发消息事件触发时
		 * @param {Object} state
		 * @param {Message[]|Message} data
		 * @returns
		 */
		pushCurrentMessageList(state, data) {
			// 还没当前会话，则跳过
			if (Array.isArray(data)) {
				// 筛选出当前会话的消息
				const result = data.filter(item => item.conversationID === state.conversationActive.conversationID)
				state.currentMessageList = [...state.currentMessageList, ...result]
			} else if (data.conversationID === state.conversationActive.conversationID) {
				state.currentMessageList = [...state.currentMessageList, data]
			}
		},
		/**
		 * 滑到顶部请求更多的历史消息
		 * */
		unshiftCurrentMessageList(state, data) {
			if (data) {
				state.currentMessageList = [...data, ...state.currentMessageList]
			}
		},
	},

	actions: {
		sendVoiceSelfCancel({
			state
		}, data) {
			// console.log('sendVoiceSelfCancel', data);
			let message = tim.tim.createCustomMessage({
				// to: this.toUserInfo.id,
				to: data.userID.toString(),
				conversationType: 'C2C',
				// 消息优先级，用于群聊（v2.4.2起支持）。如果某个群的消息超过了频率限制，后台会优先下发高优先级的消息，详细请参考：https://cloud.tencent.com/document/product/269/3663#.E6.B6.88.E6.81.AF.E4.BC.98.E5.85.88.E7.BA.A7.E4.B8.8E.E9.A2.91.E7.8E.87.E6.8E.A7.E5.88.B6)
				// 支持的枚举值：TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL（默认）, TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
				// priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
				payload: {
					data: 'voiceChatSelfCancel', // 用于标识该消息类型
					description: 'voiceChatSelfCancel', // 用于装载url
					extension: ''
				}
			});
			tim.tim.sendMessage(message);
		},
		sendVideoSelfCancel({
			state
		}, data) {
			let id = data.id || data.userID
			// console.log(id, '触发发送消息')
			let message = tim.tim.createCustomMessage({
				// to: this.toUserInfo.id,
				to: id,
				conversationType: 'C2C',
				// 消息优先级，用于群聊（v2.4.2起支持）。如果某个群的消息超过了频率限制，后台会优先下发高优先级的消息，详细请参考：https://cloud.tencent.com/document/product/269/3663#.E6.B6.88.E6.81.AF.E4.BC.98.E5.85.88.E7.BA.A7.E4.B8.8E.E9.A2.91.E7.8E.87.E6.8E.A7.E5.88.B6)
				// 支持的枚举值：TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL（默认）, TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
				// priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
				payload: {
					data: 'videoChatSelfCancel', // 用于标识该消息类型
					description: 'videoChatSelfCancel', // 用于装载url
					extension: ''
				}
			});
			tim.tim.sendMessage(message);
		},
		sendVideoChatBusy({
			state
		},id) {
			let message = tim.tim.createCustomMessage({
				// to: this.toUserInfo.id,
				to: id,
				conversationType: 'C2C',
				// 消息优先级，用于群聊（v2.4.2起支持）。如果某个群的消息超过了频率限制，后台会优先下发高优先级的消息，详细请参考：https://cloud.tencent.com/document/product/269/3663#.E6.B6.88.E6.81.AF.E4.BC.98.E5.85.88.E7.BA.A7.E4.B8.8E.E9.A2.91.E7.8E.87.E6.8E.A7.E5.88.B6)
				// 支持的枚举值：TIM.TYPES.MSG_PRIORITY_HIGH, TIM.TYPES.MSG_PRIORITY_NORMAL（默认）, TIM.TYPES.MSG_PRIORITY_LOW, TIM.TYPES.MSG_PRIORITY_LOWEST
				// priority: TIM.TYPES.MSG_PRIORITY_NORMAL,
				payload: {
					data: 'videoChatBusy', // 用于标识该消息类型
					description: 'videoChatBusy', // 用于装载url
					extension: ''
				}
			});
			tim.tim.sendMessage(message);
		}
	}
})
export default store
